package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.mz.ozy.R;
import com.mz.ozy.adapter.MsgAdapter;
import com.mz.ozy.adapter.OrderMsgAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.MsgBean;
import com.mz.ozy.bean.MsgDetailBean;
import com.mz.ozy.bean.OrderMsgBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MsgPresenter;
import com.mz.ozy.mvp.view.MsgContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.sobot.chat.ZCSobotApi;
import com.sobot.chat.api.model.Information;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MsgOrderActivity extends BaseActivity<MsgContract.View, MsgPresenter> implements MsgContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private OrderMsgAdapter mAdapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<OrderMsgBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private MeBean mMeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_msg);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText("订单消息");
        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new OrderMsgAdapter(this, mBeanList);
        mRecyclerview.setAdapter(mAdapter);
        mRefreshLayout.setEnableAutoLoadmore(true);
        mRefreshLayout.autoRefresh();
        mPresenter.getMeInfo(this, false);
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getOrderMsgList(MsgOrderActivity.this, String.valueOf(pageNo));
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getOrderMsgList(MsgOrderActivity.this, String.valueOf(pageNo));

            }
        });
    }

    @Override
    public void getOrderMsgrListSuccess(OrderMsgBean bean) {

        if (tag == 0) {
            mRefreshLayout.setLoadmoreFinished(false);
            mRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            mRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getUnreadMsgSuccess(UnreadMsgBean bean) {

    }

    @Override
    public void getMsgDetailSuccess(MsgDetailBean jsonObject) {

    }
    @Override
    public void getMsgrListSuccess(MsgBean bean) {

    }

    @Override
    public void getAgmentDetailSuccess(JSONObject jsonObject) {

    }

    @Override
    public void getgetMeInfoSuccess(MeBean bean) {
        mMeBean = bean;
    }
}
